package com.blast.apply.adapter

import android.view.View
import com.blast.apply.R
import com.chad.library.adapter.base.BaseViewHolder
import com.wxl.common.adapter.AbsKeyEventAdapter
import com.wxl.common.room.entity.UnApplyBlastEntity

class UnApplyAdapter : AbsKeyEventAdapter<UnApplyBlastEntity> {

    val widths = ArrayList<Int>()

    constructor(data: List<UnApplyBlastEntity>?) : super(R.layout.item_apply_list_layout, data)

    override fun convert(helper: BaseViewHolder, item: UnApplyBlastEntity) {

        if(this.widths.isNotEmpty()){
            setViewWidth(helper.getView(R.id.itemUnApplySerialNumberText)
                ,helper.getView(R.id.itemUnApplyNumberText)
                ,helper.getView(R.id.itemUnApplyTimeText)
                ,helper.getView(R.id.itemUnApplyTermText)
                ,helper.getView(R.id.itemUnApplyProjectText))
        }

        item?.let {
            helper.setText(
                R.id.itemUnApplySerialNumberText,
                (helper.adapterPosition + 1).toString()
            )
            helper.itemView.setBackgroundResource(if (item.isSelect) R.drawable.shape_unapply_list_item_focus else R.drawable.shape_unapply_list_item)
            it.takeIf {
                it.lgs != null && !it.lgs.lg.isNullOrEmpty()
            }?.apply {
                helper.setText(R.id.itemUnApplyNumberText, lgs.lg.size.toString())
                helper.setText(R.id.itemUnApplyTimeText, it.sqrq)
                takeIf {
                    !lgs.lg.isNullOrEmpty() && lgs.lg[0] != null
                }?.apply {
                    helper.setText(R.id.itemUnApplyTermText, lgs.lg[0].yxq)
                }
            }
            it.takeIf {
                it.licenseFileModel != null
            }?.apply {
                helper.setText(R.id.itemUnApplyProjectText, licenseFileModel.projectname)
            }
        }
    }


    fun setWidths(widths: ArrayList<Int>) {
        this.widths.clear()
        this.widths.addAll(widths)
    }


    private fun setViewWidth(vararg vs: View) {
        (0 until vs.size).forEach { i ->
            vs[i].layoutParams.width = widths[i]
        }
    }

}